<template>
  <h1>杨硕员工列表练习</h1>
  <input type="text" placeholder="请输入员工姓名" v-model="user.name">
  <input type="text" placeholder="请输入员工工资" v-model="user.salary">
  <input type="text" placeholder="请输入员工岗位" v-model="user.job">
  <button  @click="add">添加员工</button>
  <hr>
  <table border="1px">
    <tr>
      <th>序号</th>
      <th>名字</th>
      <th>工资</th>
      <th>岗位</th>
    </tr>
    <tr v-for="(u,index) in users">
      <td>{{index+1}}</td>
      <td>{{u.name}}</td>
      <td>{{u.salary}}</td>
      <td>{{u.job}}</td>
    </tr>
  </table>
</template>
<script setup>
import {ref} from "vue";
//定义员工对象用来双向绑定收集
const user=ref({
      name:'',
      salary:'',
      job:''
    });

//定义一个数组用来保存多个员工对象
const users=ref([]);
//定义方法用来保存员工数据
const add=()=>{
  //对数据进行非空校验
  if(user.value.name.trim()==''||
      user.value.salary.trim()==''||
      user.value.job.trim()==''){
    alert("请输入员工信息");
    return;
  }
  //保存员工数据
  users.value.push(user.value);
  //清空数据
  user.value={name:'',salary:'',job:''};
  console.log(users.value);
}
</script>


<style scoped>

</style>